﻿using FreesenseSubAIS.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FreesenseSubAIS.UI
{
    public partial class LogInForm : Form
    {
        private int times=0;
        public LogInForm()
        {
            InitializeComponent();
        }

        private void Longin_Btn_Click(object sender, EventArgs e)
        {
            times++;
            if (userpassw_tx.Text != "")
            {
                if (Device.Parameter.PassWord == null)
                    Device.Parameter.PassWord = "freesense";
                if (userpassw_tx.Text == Device.Parameter.PassWord)
                {
                    Device.EnginerModel = true;
                    msg.Text = "操作成功！";
                    timer1.Enabled = true;
                    timer1.Start();
                }
                else
                {
                    userpassw_tx.Text = "";
                    toolTip1.BackColor = Color.Red;
                    toolTip1.Show(times > 10 ? "已经多次输错密码，试试联系工程师？" : "密码错误！", this.userpassw_tx, 10, 10, 1000);
                    userpassw_tx.Focus();
                }
            }
            else
            {
                userpassw_tx.Focus();
                toolTip1.Show("密码不能为空，请重新输入！", this.userpassw_tx, 10, 20, 1000);
            }
        }

        private void Modfiy_Btn_Click(object sender, EventArgs e)
        {
            if (userpassw_tx.Text == Device.Parameter.PassWord)
            {
                if (pwconfir_tx.Text != "" && confirmps_tx.Text != "" && pwconfir_tx.Text == confirmps_tx.Text)
                {
                    Device.Parameter.PassWord = pwconfir_tx.Text;
                    Device.SaveParameter();
                    msg.Text = "操作成功！";
                    timer1.Enabled = true;
                    timer1.Start();
                }
                else
                {
                    toolTip1.Show("修改失败，请重新输入新密码！", this.userpassw_tx, 10, 20, 1000);
                }
            }
            else
                toolTip1.Show("密码错误！", this.userpassw_tx, 10, 20, 1000);
           
        }

        private void Cancle_Btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            times++;
            if(times >10)
            this.Close();
        }

        private void LogInForm_Load(object sender, EventArgs e)
        {
            userpassw_tx.Focus();
        }
    }
}
